import 'package:flutter/material.dart';
import 'package:hixing_app/const/color_res.dart';
import 'package:hixing_app/widgets/custom_indicator.dart';

class CommonTabbar extends StatefulWidget {
  const CommonTabbar(
      {super.key,
      required this.tabList,
      this.bgColor,
      required this.controller,
      this.marginLeft = 0,
      this.boxMarginLeft = 8.0,
      this.onTap});

  final List<String> tabList;
  final TabController controller;
  final Color? bgColor;
  final void Function(int)? onTap;
  final double marginLeft;
  final double boxMarginLeft;

  @override
  _CommonTabbarState createState() => _CommonTabbarState();
}

class _CommonTabbarState extends State<CommonTabbar> {
  late final ThemeData _themeData = Theme.of(context);
  MyCustomIndicator _indicator = MyCustomIndicator();

  @override
  Widget build(BuildContext context) {
    return Container(
      // padding: const EdgeInsets.only(bottom: 4),
      color: widget.bgColor,
      margin: EdgeInsets.only(right: 0, left: widget.boxMarginLeft),
      height: 36,
      child: Row(
        children: [
          Expanded(
            child: TabBar(
                dividerColor: Colors.transparent,
                labelPadding: const EdgeInsets.symmetric(horizontal: 8),
                unselectedLabelColor: ColorRes.greyText7B,
                labelColor: _themeData.primaryColor,
                controller: widget.controller,
                indicator: _indicator,
                onTap: (v) {
                  if (widget.marginLeft != 0) {
                    setState(() {
                      _indicator = MyCustomIndicator(
                          marginLeft: v == 0 ? 0 : widget.marginLeft);
                    });
                  }
                  if (widget.onTap != null) {
                    widget.onTap!(v);
                  }
                },
                labelStyle: const TextStyle(
                    fontWeight: FontWeight.normal, fontSize: 14),
                isScrollable: false,
                tabs: List.generate(
                    widget.tabList.length,
                    (index) => Container(
                          margin: EdgeInsets.only(
                              left: index == 0 ? 0 : widget.marginLeft),
                          child: Tab(
                            child: Text(widget.tabList[index]),
                          ),
                        ))),
          ),
        ],
      ),
    );
  }
}
